package com.asion.controller;

import com.asion.domain.Res.BaseRes;
import com.asion.domain.Res.VideoDetailRes;
import com.asion.domain.Res.VideoRes;
import com.asion.mapper.VideoMapper;
import com.asion.service.TaskDetailService;
import com.asion.service.TaskService;
import com.asion.service.UserService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.lang.reflect.Type;
import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @Autowired
    private TaskDetailService taskDetailService;

    @RequestMapping("/init")
    private void init(){
        for(int page = 1;page<=217;page++){
            getVideoList(page);
        }
    }
    @Async("getVideoList")
    public void getVideoList(int page){
        System.out.println("执行任务-------------------"+ page);
        RestTemplate restTemplate=new RestTemplate();
        UriComponents uriComponents= UriComponentsBuilder.fromUriString(
                        "https://subocaiji.com/api.php/provide/vod/from/subm3u8/at/json?t=27&pg="+page)
                .build()
                .expand("dodo")
                .encode();
        URI uri=uriComponents.toUri();
        String responseEntity=restTemplate.getForEntity(uri,String.class).getBody();
        System.out.println(responseEntity);
        Gson gson = new Gson();
        Type listType = new TypeToken<BaseRes<VideoRes>>(){}.getType();
        BaseRes<VideoRes> fromJson = gson.fromJson(responseEntity,listType);
        List<VideoRes> list= fromJson.getList();
        String ids = "";
        for (int i = 0;  i<list.size();i++){
            VideoRes video = list.get(i);
            ids +=video.getVod_id()+",";
        }
        initDetail(ids);

        taskService.saveOrUpdateBatch(list);
    }
    @Async("initDetail")
    public void initDetail(String ids){
        RestTemplate restTemplate=new RestTemplate();
        UriComponents uriComponents= UriComponentsBuilder.fromUriString(
                        "https://subocaiji.com/api.php/provide/vod/from/subm3u8/at/json?ac=detail&ids="+ids)
                .build()
                .expand("dodo")
                .encode();
        URI uri=uriComponents.toUri();
        String responseEntity=restTemplate.getForEntity(uri,String.class).getBody();
        System.out.println(responseEntity);
        Gson gson = new Gson();
        Type listType = new TypeToken<BaseRes<VideoDetailRes>>(){}.getType();
        BaseRes<VideoDetailRes> fromJson = gson.fromJson(responseEntity,listType);
        List<VideoDetailRes> list= fromJson.getList();
        for (int i = 0;  i<list.size();i++){
            VideoDetailRes video = list.get(i);
        }

        taskDetailService.saveOrUpdateBatch(list);
    }
}
